package cn.devezhao.bizz.security;

import cn.devezhao.bizz.privileges.DepthEntry;
import cn.devezhao.bizz.privileges.Permission;
import cn.devezhao.bizz.privileges.Privileges;
import cn.devezhao.bizz.privileges.impl.BizzDepthEntry;
import cn.devezhao.bizz.privileges.impl.BizzPermission;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

/**
 * @author WangRuiJie
 * @Title: EntityPrivileges
 * @Package cn.devezhao.bizz.security
 * @Description:
 * @date 2022/1/205:01 下午
 */
public class EntityPrivileges implements Privileges {
    private static final long serialVersionUID = -8141823128069571526L;
    private final Integer entity;
    private final String definition;
    private final Map<DepthEntry, Permission[]> dePermissions = new HashMap();
    private final Set<Permission> allPermissions = new HashSet();

    public EntityPrivileges(Integer entity, String definition) {
        this.entity = entity;
        this.definition = definition;
        String[] depthValues = definition.split(",");
        String[] var4 = depthValues;
        int var5 = depthValues.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String dv = var4[var6];
            String[] dpVal = dv.split(":");
            Permission[] perms = BizzPermission.parse(Integer.parseInt(dpVal[1]));
            this.allPermissions.addAll(Arrays.asList(perms));
            this.dePermissions.put(BizzDepthEntry.parse(Integer.parseInt(dpVal[0])), perms);
        }

    }

    public Serializable getIdentity() {
        return this.getEntity();
    }

    public boolean allowed(Permission action) {
        return this.allPermissions.contains(action);
    }

    public boolean allowed(Permission action, Serializable targetGuard) {
        throw new UnsupportedOperationException();
    }

    public DepthEntry superlative(Permission action) {
        Set<DepthEntry> set = new HashSet(this.dePermissions.size());
        Iterator var3 = this.dePermissions.entrySet().iterator();

        while(true) {
            while(var3.hasNext()) {
                Entry<DepthEntry, Permission[]> e = (Entry)var3.next();
                Permission[] var5 = (Permission[])e.getValue();
                int var6 = var5.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    Permission p = var5[var7];
                    if (action.equals(p)) {
                        set.add(e.getKey());
                        break;
                    }
                }
            }

            if (set.isEmpty()) {
                return BizzDepthEntry.NONE;
            }

            DepthEntry sup = BizzDepthEntry.NONE;
            Iterator var10 = set.iterator();

            do {
                if (!var10.hasNext()) {
                    return sup;
                }

                DepthEntry de = (DepthEntry)var10.next();
                if (de.getMask() > sup.getMask()) {
                    sup = de;
                }
            } while(!BizzDepthEntry.GLOBAL.equals(sup));

            return BizzDepthEntry.GLOBAL;
        }
    }

    public Integer getEntity() {
        return this.entity;
    }

    public String getDefinition() {
        return this.definition;
    }
}
